package hk.ust.cse.comp3111.studentSchedule;



import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ImportByCourse_2 extends Fragment {
	
	TextView courseCodeSelected;
	TextView title;
	CourseDatabaseHandler db;
	Spinner spinner1, spinner2, spinner3;
	int structure;
	
	List<String> list1= new ArrayList<String>();
    List<String> list2= new ArrayList<String>();
    List<String> list3= new ArrayList<String>();
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.import_by_course_2, container, false);
         
        return rootView;
    }
	
	
	@Override
    public void onStart() {
    	super.onStart();
    	
    	Bundle args = getArguments();
    	String courseCode=args.getString("SELECTED");
    	courseCodeSelected=(TextView)this.getView().findViewById(R.id.courseCodeSelected);
    	title=(TextView)this.getView().findViewById(R.id.title);
    	courseCodeSelected.setText(courseCode.toString().toUpperCase());
    	//Toast.makeText(getActivity(),"HEHE "+word,Toast.LENGTH_SHORT).show();
    	
    	db=new CourseDatabaseHandler(this.getActivity());
    	List<Course> courseQuery = db.getCourseByCourseCode(courseCode.toString().toUpperCase());
    	title.setText(courseQuery.get(0).getCourseName());
    	//Toast.makeText(getActivity(),"HEHE "+title.get(0).getCourseName(),Toast.LENGTH_SHORT).show();
    	
    	courseCodeSelected.setTextColor(Color.parseColor("#6c00d7"));
    	title.setTextColor(Color.parseColor("#6c00d7"));
    	structure = courseQuery.get(0).getStructure();
    	
    	spinner1 =  (Spinner) this.getView().findViewById(R.id.spinner1);
    	spinner2 =  (Spinner) this.getView().findViewById(R.id.spinner2);
    	spinner3 =  (Spinner) this.getView().findViewById(R.id.spinner3);
    	spinner1.setVisibility(View.INVISIBLE);
    	spinner2.setVisibility(View.INVISIBLE);
    	spinner3.setVisibility(View.INVISIBLE);
    	
    	ArrayAdapter<String> adapter;
        
    	operate(structure,courseQuery);
    	
    	String[] string1=list1.toArray(new String[list1.size()]);
    	String[] string2=list2.toArray(new String[list2.size()]);
    	String[] string3=list3.toArray(new String[list3.size()]);
    	
    	if(structure==1 || structure==5 || structure==6)	{
    		adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item,list1);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(adapter);
            spinner1.setVisibility(View.VISIBLE);
    	}
		else if (structure==2 || structure==4 ||structure==7)	{
			adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item,list1);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(adapter);
            
            adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item,list2);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(adapter);
            
            spinner1.setVisibility(View.VISIBLE);
            spinner2.setVisibility(View.VISIBLE);
			
		}
		else if(structure==3)	{
			adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item,list1);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(adapter);
            
            adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item,list2);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(adapter);
            
            adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item,list3);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner3.setAdapter(adapter);
            
            spinner1.setVisibility(View.VISIBLE);
            spinner2.setVisibility(View.VISIBLE);
            spinner3.setVisibility(View.VISIBLE);
		}
    	
    	
    	
	}
	
	public void operate(int structure, List<Course> courseQuery){
		
		int numberOfType=0;
		
		if(structure==1 || structure==5 || structure==6)	numberOfType=1;
		else if (structure==2 || structure==4 ||structure==7)	numberOfType=2;
		else if(structure==3)	numberOfType=3;
	
        if(structure==1 || structure==5 || structure==6){
        	for (Course c : courseQuery){
        		list1.add(c.getSection());
        	}
        }
        
        else if (structure==2){
        	for (Course c : courseQuery){
        		if(c.getType()==1) list1.add(c.getSection());
        		else list2.add(c.getSection());
        	}
        }
        
        else if (structure==4){
        	for (Course c : courseQuery){
        		if(c.getType()==1) list1.add(c.getSection());
        		else list2.add(c.getSection());
        	}
        }
        
		else if (structure==7){
			for (Course c : courseQuery){
        		if(c.getType()==1) list1.add(c.getSection());
        		else list2.add(c.getSection());
        	}
        }
        
		else if (structure==3){
			for (Course c : courseQuery){
        		if(c.getType()==1) list1.add(c.getSection());
        		else if(c.getType()==2) list2.add(c.getSection());
        		else list3.add(c.getSection());
        	}
		}

		
	}
	

}
